package com.xm;

/**
 * @Author：xm
 * @Project：runoob
 * @Date：2024/7/22 0:08
 */

public class Runnable10 implements Runnable {
    private String threadName;
    private Thread t;

    public Runnable10(String name) {
        threadName = name;
        System.out.println("Creating " + threadName);
    }

    @Override
    public void run() {
        System.out.println("Running " + threadName);
        try {
            for (int i = 4; i > 0; i--) {
                System.out.println("Thread：" + threadName + "，" + i);
                // 线程睡眠
                Thread.sleep(50);
            }
        } catch (InterruptedException e) {
            System.out.println("Thread " + threadName + " interrupted");
        }
        System.out.println("Thread " + threadName + " exit");
    }

    public void start() {
        System.out.println("Starting " + threadName);
        if (t == null) {
            t = new Thread(this, threadName);
            System.out.println("t = " + t);
            System.out.println("t.getName() = " + t.getName());
            t.start();
        }
    }

    public static void main(String[] args) {
        Runnable10 R1 = new Runnable10("Thread-1");
        R1.start();

        Runnable10 R2 = new Runnable10("Thread-2");
        R2.start();
    }
}
